<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="主键" prop="id">
        <el-input
          v-model="queryParams.id"
          placeholder="请输入主键"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="任务名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入任务名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="任务状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择任务状态" clearable>
          <el-option
            v-for="dict in dict.type.sys_notice_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:syncConfig:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:syncConfig:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="syncConfigList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="任务编号" align="center" prop="id" width="80" />
      <el-table-column label="任务名称" align="center" prop="name"  width="220" />
      <el-table-column label="任务状态" align="center" prop="status"  width="80">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_notice_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" width="180" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button v-show="scope.row.status == '1'"
               size="mini"
               type="text"
               @click="updateStatus(0,scope.row)"
               v-hasPermi="['system:syncConfig:edit']"
          >启用</el-button>
          <el-button v-show="scope.row.status == '0'"
               size="mini"
               type="text"
               @click="updateStatus(1,scope.row)"
               v-hasPermi="['system:syncConfig:edit']"
          >停用</el-button>
          <el-button v-show="scope.row.status == '1'"
            size="mini"
            type="text"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:syncConfig:edit']"
          >编辑</el-button>
          <el-button
            size="mini" v-show="scope.row.status == '1'"
            type="text"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:syncConfig:remove']"
          >删除</el-button>
          <el-button v-show="scope.row.status == '0'"
            size="mini"
            type="text"
            @click="handleLog(scope.row)"
          >异常日志</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

  </div>
</template>

<script>
import {listSyncConfig, delSyncConfig, start, end, updateSyncConfig} from "@/api/system/syncConfig";

export default {
  name: "SyncConfig",
  dicts: ['sys_notice_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 同步任务管理表格数据
      syncConfigList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        id: null,
        name: null,
        mUrl: null,
        mPort: null,
        mUser: null,
        mPass: null,
        eHost: null,
        eUrl: null,
        ePort: null,
        eUser: null,
        ePass: null,
        syncData: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询同步任务管理列表 */
    getList() {
      this.loading = true;
      listSyncConfig(this.queryParams).then(response => {
        this.syncConfigList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        mUrl: null,
        mPort: null,
        mUser: null,
        mPass: null,
        eHost: null,
        eUrl: null,
        ePort: null,
        eUser: null,
        ePass: null,
        syncData: null,
        status: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.$store.dispatch("tagsView/delView", this.$route); //关闭当前页面
      this.$router.push({ path: '/system/syncConfig/add', query: {} })
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      const id = row.id || this.ids
      this.$store.dispatch("tagsView/delView", this.$route); //关闭当前页面
      this.$router.push({ name: 'editSyncConfig', params: {id:id} })
    },
    updateStatus(type,row){
      if(type == 0){
        this.$confirm('是否确定启用该任务?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          //启用
          row.status = type;
          start(row.id).then(response => {
            updateSyncConfig(row).then(response => {
              this.$modal.msgSuccess("启用成功");
              this.getList();
            });
          });
        }).catch(() => {});
      }else{
        this.$confirm('是否确定停用该任务?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          //停用
          row.status = type;
          end(row.id).then(response => {
            updateSyncConfig(row).then(response => {
              this.$modal.msgSuccess("停用成功");
              this.getList();
            });
          });
        }).catch(() => {});
      }
    },
    /** 查看日志 */
    handleLog(row){
      //进入到日志页面
      const id = row.id || this.ids
      this.$store.dispatch("tagsView/delView", this.$route); //关闭当前页面
      this.$router.push({ name: 'Log', params: {id:id} })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除任务编号为"' + ids + '"的数据项？').then(function() {
        return delSyncConfig(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/syncConfig/export', {
        ...this.queryParams
      }, `syncConfig_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
